package com.ecoolex.union.pay.retailer.admin.graphql;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.lang.NonNull;

import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.util.Assert;
import com.ecoolex.framework.common.util.BeanUtil;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.modularize.api.auth.admin.AdminMemberAuthority;
import com.ecoolex.framework.modularize.api.auth.annotation.AdminAuthoritativeRegister;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module;
import com.ecoolex.framework.web.api.graphql2.annotation.MutationField;
import com.ecoolex.framework.web.api.graphql2.annotation.QueryField;
import com.ecoolex.union.pay.retailer.admin.core.AdminConstant;
import com.ecoolex.union.pay.retailer.entity.UnionPayAutoReceiveCouponConfig;
import com.ecoolex.union.pay.retailer.pojo.AddUnionPayAutoReceiveCouponConfigDTO;
import com.ecoolex.union.pay.retailer.pojo.EditUnionPayAutoReceiveCouponConfigDTO;
import com.ecoolex.union.pay.retailer.ws.UnionPayAutoReceiveCouponConfigService;

@GraphQL2Module(type = GraphQL2Module.Type.Admin, description = "银联自动领券配置管理")
@AdminAuthoritativeRegister(parentKey = AdminConstant.AUTH_MODULE_KEY, 
key = UnionPayAutoReceiveCouponConfigGraphql.AUTH, 
name = "银联自动领券配置管理")
public class UnionPayAutoReceiveCouponConfigGraphql {

	public static final String AUTH = AdminConstant.AUTH_MODULE_KEY + "-AUTO-RECEIVE-COUPON-CONFIG";

    public static final String AUTH_ADD = AUTH + "-ADD";

    public static final String AUTH_EDIT = AUTH + "-EDIT";

    public static final String AUTH_DEL = AUTH + "-DEL";

    public static final String AUTH_GET = AUTH + "-GET";

    public static final String AUTH_QUERY = AUTH + "-QUERY";
    
    @Reference
    private UnionPayAutoReceiveCouponConfigService service;
    
    @QueryField(field = "unionPayAutoReceiveCouponConfig",description = "银联自动领券配置")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_GET, name = "银联自动领券配置")
    public UnionPayAutoReceiveCouponConfig get(@NonNull Integer configId, AdminMemberAuthority user) {
    	user.assertAuth(AUTH_GET);
    	return service.get(configId);
    }
    
    @QueryField(field = "unionPayAutoReceiveCouponConfigList",description = "银联自动领券配置列表")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_QUERY, name = "银联自动领券配置列表")
    public Pagination<UnionPayAutoReceiveCouponConfig> query(String channelId, String couponCode, @NonNull Page page, AdminMemberAuthority user){
    	user.assertAuth(AUTH_QUERY);
    	return service.query(channelId, couponCode, page);
    }
    
    @MutationField(field = "addUnionPayAutoReceiveCouponConfig", description = "添加银联自动领券配置")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_ADD, name = "添加银联自动领券配置")
    public UnionPayAutoReceiveCouponConfig add(@NonNull AddUnionPayAutoReceiveCouponConfigDTO add, AdminMemberAuthority user) {
    	user.assertAuth(AUTH_ADD);
    	return service.add(add, user.userName());
    }
    
    @MutationField(field = "editUnionPayAutoReceiveCouponConfig", description = "编辑银联自动领券配置")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_EDIT, name = "编辑银联自动领券配置")
    public UnionPayAutoReceiveCouponConfig edit(@NonNull EditUnionPayAutoReceiveCouponConfigDTO edit,AdminMemberAuthority user) {
    	user.assertAuth(AUTH_EDIT);
    	UnionPayAutoReceiveCouponConfig config = service.get(edit.getConfigId());
    	Assert.isTrue(Check.notNull(config),ResultCode.VERIFY.build(1), "配置不存在");
    	BeanUtil.copy(edit, config);
    	return service.edit(config, user.userName());
    }
    
    @MutationField(field = "delUnionPayAutoReceiveCouponConfig", description = "删除银联自动领券配置")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_DEL, name = "删除银联自动领券配置")
    public Boolean delete(@NonNull Integer configId, AdminMemberAuthority user) {
    	user.assertAuth(AUTH_DEL);
    	UnionPayAutoReceiveCouponConfig config = service.get(configId);
    	Assert.isTrue(Check.notNull(config),ResultCode.VERIFY.build(1), "配置不存在");
    	return service.delete(config, user.userName());
    }
}
